package com.example.administrator.liveapp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.MessageRemindBean;

import java.util.List;

/**
 * Created by Administrator on 2017/9/27 0027.
 */

public class MessageRemindAdapter extends RecyclerView.Adapter<MessageRemindAdapter.ViewHolder> implements View.OnClickListener {
    public List<MessageRemindBean.NotifyBean> notify;
    public MessageRemindAdapter(List<MessageRemindBean.NotifyBean> notify) {
        this.notify = notify;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_remind_item,null);
        ViewHolder holder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.message_text.setText((position+1)+". "+notify.get(position).getNotifycontent());
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return notify.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView message_text;
        public ViewHolder(View itemView) {
            super(itemView);
            message_text = (TextView) itemView.findViewById(R.id.message_text);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    private OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
